home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / misc / FGP210.lha / FGP.c < prev    next >
C/C++ Source or Header  |  1994-09-27  |  34KB  |  1,337 lines

  1. /***************************
  2.  * Fantasy Grand Prix v2.0 *
  3.  *   © 1994 Simon Austin   *
  4.  ***************************/
  5.  
  6. /* Includes */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12.  
  13. /* Defines */
  14.  
  15. #define DRIVERS 100
  16. #define CHASSIS 50
  17. #define ENGINES 50
  18. #define MAXTEAM 100
  19.  
  20. /* Variables */
  21.  
  22. char *ver = "$VER: FGP v2.10 (18/9/94) © S Austin"; /* Amiga Version String */
  23. char *verst = "FGP2 by S Austin, additional design by J Simpson.\n";
  24. char *errst = "Usage: FGP <[-r[n]] [-t[n]] [-d[n]] [filename]>\n";
  25.  
  26. FILE *data_file, *teams_file, *scores_file, *drivers_file, *old_scores,
  27.      *chassis_file, *engine_file; /* Pointers to various files */
  28.  
  29. FILE *grid_file, *results_file, *other_file; /* Old data file pointers */
  30.  
  31. char data_name[256], grid_name[256], results_name[256], other_name[256];
  32.  
  33. char *teams_name = "teams.fgp2", *scores_name = "scores.fgp2", 
  34.      *drivers_name = "drivers.fgp2", *chassis_name = "chassis.fgp2",
  35.      *engine_name = "engine.fgp2", *temp_name = "_tempscores";
  36.  
  37. char store[100], car[100], eng[100], fullname[100], name[100], fnam[100],
  38.      racename[100], code[4], racecheck[100];
  39.  
  40. char owner[MAXTEAM][100], teamname[MAXTEAM][100], namea[DRIVERS][100],
  41.      fnama[DRIVERS][100], chassisa[CHASSIS][100], enginesa[ENGINES][100],
  42.      racenames[16][100];
  43.  
  44. int  highest, highscore; /* used to produce league table */
  45.  
  46. int  driver1[MAXTEAM], driver2[MAXTEAM], driver3[MAXTEAM], 
  47.      chassis[MAXTEAM],engine[MAXTEAM], scores[MAXTEAM], thisrce[MAXTEAM]; 
  48.                                             /* Team details */
  49.  
  50. int  eflags[ENGINES], cflags[CHASSIS], sflags[MAXTEAM]; 
  51.                                      /* Flags for points calculation */
  52.  
  53. int  grid[DRIVERS], position[DRIVERS], status[DRIVERS], warmup[DRIVERS], 
  54.      nominated, finisher, qualifier; /* Results */
  55.  
  56. int  topsixpts[DRIVERS], awardpts[DRIVERS], incgridpts[DRIVERS],
  57.      warmuppts[DRIVERS], racesc[DRIVERS], retirepts[DRIVERS],
  58.      noqualpts[DRIVERS], carscr[CHASSIS], engscr[ENGINES],
  59.      carpts[DRIVERS], engpts[DRIVERS]; /* Scores */
  60.  
  61. int  drcar[DRIVERS], dreng[DRIVERS], whichcar[DRIVERS], whicheng[DRIVERS]; 
  62.                                    /* chassis/engine for each driver */
  63.  
  64. int  teamscores[MAXTEAM][16], driverscore[DRIVERS][16], 
  65.      drivertotal[DRIVERS];
  66.  
  67. int  points[] = { 0, 10, 6, 4, 3, 2, 1 }; /* Points for pos' 1-6 */
  68.   
  69. int  last, nteams, ndrivers, i, j, k, pos, point, normal, teams, drive, 
  70.      number, totwarm, exists_flag, racedrivers, old_grid, old_car, old_eng,
  71.      maxdrivers, maxchassis, maxengines, nraces;
  72.  
  73. char state, nom; /* variables used to work out flags */
  74. int  end, warm;
  75.  
  76. int  inlen = 80; /* Max input length */
  77.  
  78. void disperr(char *);
  79. void errorinfile(char *, char *, int);
  80. int driver2no(char *);
  81. int chassis2no(char *);
  82. int engine2no(char *);
  83. int strcasencmp(char *, char *, int);
  84. void capitalise(char *);
  85. unsigned int readline(char *, int, FILE *);
  86.   
  87. main(int argc, char *argv[])
  88.   if(argc < 2 || argc > 5)
  89.   { 
  90.     /* Incorrect command line, display message and quit */
  91.     fputs(verst, stderr);
  92.     fputs(errst, stderr);
  93.     exit(0);
  94.   }
  95.   else
  96.   {   
  97.     /* clear output flags */
  98.     normal = 255;
  99.     teams = 255;
  100.     drive = 255;
  101.     strcpy(data_name, "no_name");
  102.     
  103.     for(i = 1; i < argc; i+=1)
  104.     {
  105.       if(!strcasencmp(argv[i], "-r", 2))
  106.       { 
  107.         if(normal == 255)
  108.         {
  109.           /* Normal output wanted */
  110.           strncpy(argv[i], "00", 2);
  111.           normal = atoi(argv[i]);
  112.         }
  113.         else
  114.         {
  115.           /* Incorrect command line, display message and quit */
  116.           fputs(verst, stderr);
  117.           fputs(errst, stderr);
  118.           exit(0);
  119.         }
  120.       }
  121.       else if(!strcasencmp(argv[i], "-t", 2))
  122.       {
  123.         if(teams == 255)
  124.         {
  125.           /* Teams output wanted */
  126.           strncpy(argv[i], "00", 2);
  127.           teams = atoi(argv[i]);
  128.         }
  129.         else
  130.         {
  131.           /* Incorrect command line, display message and quit */
  132.           fputs(verst, stderr);
  133.           fputs(errst, stderr);
  134.           exit(0);
  135.         }
  136.       }
  137.       else if(!strcasencmp(argv[i], "-d", 2))
  138.       {
  139.         if(drive == 255)
  140.         {
  141.           /* Driver output wanted */
  142.           strncpy(argv[i], "00", 2);
  143.           drive = atoi(argv[i]);
  144.         }
  145.         else
  146.         {
  147.           /* Incorrect command line, display message and quit */
  148.           fputs(verst, stderr);
  149.           fputs(errst, stderr);
  150.           exit(0);
  151.         }
  152.       }
  153.       else
  154.       {
  155.         if(!strcmp(data_name, "no_name"))
  156.         {
  157.           /* Set data filename */
  158.           strcpy(data_name, argv[i]);
  159.         }
  160.         else
  161.         {
  162.           /* Incorrect command line, display message and quit */
  163.           fputs(verst, stderr);
  164.           fputs(errst, stderr);
  165.           exit(0);
  166.         }
  167.       }
  168.     }  
  169.   }
  170.  
  171.   /* Clear arrays */
  172.   for(i = 1; i < DRIVERS; i+=1)
  173.   {
  174.     grid[i] = 0;
  175.     position[i] = 0;
  176.     status[i] = 0;
  177.     drcar[i] = 0;
  178.     dreng[i] = 0;
  179.     warmup[i] = 0;
  180.     topsixpts[i] = 0;
  181.     awardpts[i] = 0;
  182.     incgridpts[i] = 0;
  183.     warmuppts[i] = 0;
  184.     retirepts[i] = 0;
  185.     noqualpts[i] = 0;
  186.     carpts[i] = 0;
  187.     engpts[i] = 0;
  188.     racesc[i] = 0;
  189.   }
  190.       
  191.   for(i = 0; i <= CHASSIS; i+=1)
  192.   {
  193.     carscr[i] = 0;
  194.   }
  195.       
  196.   for(i = 0; i <= ENGINES; i+=1)
  197.   {
  198.     engscr[i] = 0;
  199.   }
  200.  
  201.   for(i = 0; i <= MAXTEAM; i+=1)
  202.   {
  203.     scores[i] = 0;
  204.   }
  205.  
  206.   /* Read in chassis names */
  207.   chassis_file = fopen(chassis_name, "r");
  208.   if(chassis_file == 0)
  209.   {
  210.     disperr(chassis_name);
  211.     exit(0);
  212.   }
  213.   
  214.   i=1;
  215.   readline(store, inlen, chassis_file);
  216.   while(i <= CHASSIS && !feof(chassis_file))
  217.   {
  218.     strcpy(chassisa[i], store);
  219.     readline(store, inlen, chassis_file);
  220.     i+=1;
  221.   }
  222.   maxchassis = i-1;
  223.   
  224.   /* Read in engines names */
  225.   engine_file = fopen(engine_name, "r");
  226.   if(engine_file == 0)
  227.   {
  228.     disperr(engine_name);
  229.     exit(0);
  230.   }
  231.   
  232.   i=1;
  233.   readline(store, inlen, engine_file);
  234.   while(i <= ENGINES && !feof(engine_file))
  235.   {
  236.     strcpy(enginesa[i], store);
  237.     readline(store, inlen, engine_file);
  238.     i+=1;
  239.   }
  240.   maxengines = i-1;
  241.  
  242.   fclose(engine_file);
  243.  
  244.   /* Read in driver details */
  245.   drivers_file = fopen(drivers_name, "r");
  246.   if(drivers_file == 0)
  247.   {
  248.     disperr(drivers_name);
  249.     exit(0);
  250.   }
  251.   
  252.   i=1;
  253.   readline(store, inlen, drivers_file);
  254.   while(i <= DRIVERS && !feof(drivers_file))
  255.   {
  256.     strcpy(namea[i], store);
  257.     readline(store, inlen, drivers_file);
  258.     strcpy(fnama[i], store);
  259.     readline(store, inlen, drivers_file);
  260.     whichcar[i] = chassis2no(store);
  261.     readline(store, inlen, drivers_file);
  262.     whicheng[i] = engine2no(store);
  263.     readline(store, inlen, drivers_file);
  264.     i+=1;
  265.   }
  266.   maxdrivers = i-1;
  267.   
  268.   fclose(drivers_file);
  269.  
  270.   if(strcmp(data_name, "no_name") != 0)
  271.   {
  272.     /* Get details from data file */
  273.     data_file = fopen(data_name, "r");
  274.     if(data_file == 0)
  275.     {
  276.       /* file named on command line does not exist */
  277.       /* try old style files */
  278.       /* Produce names of three input files */
  279.       strcpy(grid_name, data_name);
  280.       strcat(grid_name, ".grid");
  281.       strcpy(results_name, data_name);
  282.       strcat(results_name, ".rslt");
  283.       strcpy(other_name, data_name);
  284.       strcat(other_name, ".xtra");
  285.  
  286.       /* Use root name of file as race name */
  287.       strcpy(racename, data_name);
  288.  
  289.       /* open and parse starting grid file */
  290.       grid_file = fopen(grid_name, "r");
  291.       if(grid_file == 0)
  292.       {
  293.         disperr(grid_name);
  294.         exit(0);
  295.       }
  296.  
  297.       readline(store, inlen, grid_file);
  298.       ndrivers = atoi(store);
  299.  
  300.       for(i = 1; i < ndrivers+1; i+=1)
  301.       {
  302.         readline(store, inlen, grid_file);
  303.         grid[i] = driver2no(store);
  304.         if(grid[i] == 0)
  305.         {
  306.           errorinfile(grid_name, "Unknown driver", i+1);
  307.           exit(0);
  308.         }
  309.         drcar[i] = whichcar[grid[i]];
  310.         dreng[i] = whicheng[grid[i]];
  311.       }
  312.  
  313.       fclose(grid_file);
  314.     
  315.       /* open and parse other details file */
  316.       other_file = fopen(other_name, "r");
  317.       if(other_file == 0)
  318.       {
  319.         disperr(other_name);
  320.         exit(0);
  321.       }
  322.  
  323.       for(i = 1; i < 7; i+=1)
  324.       {
  325.         readline(store, inlen, other_file);
  326.         number = driver2no(store);
  327.         if(number == 0)
  328.         {
  329.           errorinfile(other_name, "Unknown driver", i);
  330.           exit(0);
  331.         }
  332.         j = 0;
  333.         do
  334.         {
  335.           j+=1;
  336.         }
  337.         while(grid[j] != number);
  338.         warmup[j] = i;
  339.       }
  340.       readline(store, inlen, other_file);
  341.       nominated = driver2no(store);
  342.       if(nominated > maxdrivers)
  343.       {
  344.         errorinfile(other_name, "Illegal nominated driver", 7);
  345.         exit(0);
  346.       }
  347.       readline(store, inlen, other_file);
  348.       finisher = atoi(store);
  349.       if(finisher > ndrivers)
  350.       {
  351.         errorinfile(other_name, "Illegal final classified driver", 8);
  352.         exit(0);
  353.       }
  354.       readline(store, inlen, other_file);
  355.       qualifier = atoi(store);
  356.       if(qualifier > ndrivers)
  357.       {
  358.         errorinfile(other_name, "Illegal last qualified driver", 9);
  359.         exit(0);
  360.       }
  361.       if(qualifier < finisher)
  362.       {
  363.         errorinfile(other_name, "Final classified/last qualified driver mismatch", 8);
  364.         exit(0);
  365.       }
  366.       totwarm = 6;
  367.   
  368.       fclose(other_file);
  369.  
  370.       /* open and parse final results file */
  371.       results_file = fopen(results_name, "r");
  372.       if(results_file == 0)
  373.       {
  374.         disperr(results_name);
  375.         exit(0);
  376.       }
  377.  
  378.       for(i = 1; i < qualifier+1; i+=1)
  379.       {
  380.         readline(store, inlen, results_file);
  381.         number = driver2no(store);
  382.         if(number == 0)
  383.         {
  384.           errorinfile(results_name, "Unknown driver", i);
  385.           exit(0);
  386.         }
  387.         j = 0;
  388.         do
  389.         {
  390.           j+=1;
  391.         }
  392.         while(grid[j] != number);
  393.         position[j] = i;
  394.       }
  395.  
  396.       fclose(results_file);
  397.   
  398.       for(i = 1; i < ndrivers+1; i+=1)
  399.       {
  400.         if(position[i] <= finisher)
  401.           status[i] = 1;
  402.         if(position[i] > finisher && position[i] <= qualifier)
  403.           status[i] = 2;
  404.         if(position[i] == 0)
  405.           status[i] = 3;
  406.       }
  407.     }
  408.     else
  409.     {
  410.       /* read in name of race */
  411.       readline(racename, inlen, data_file);
  412.  
  413.       i = 0;
  414.       totwarm = 0;
  415.       nominated = 0;
  416.       readline(store, inlen, data_file);
  417.  
  418.       do
  419.       {
  420.         i+=1;
  421.         grid[i] = driver2no(store);
  422.         if(grid[i] == 0)
  423.         {
  424.           errorinfile(data_name, "Unknown driver", i*2);
  425.           exit(0);
  426.         }
  427.         drcar[i] = whichcar[grid[i]];
  428.         dreng[i] = whicheng[grid[i]];
  429.       
  430.         /* Check finished/retired/DNQ/dis-qualified flag */
  431.         readline(store, inlen, data_file);
  432.         sscanf(store, "%c %d %d %c", &state, &end, &warm, &nom);
  433.        
  434.         state = tolower(state);
  435.         status[i] = 4;
  436.         if(state == 'd')
  437.           status[i] = 0;
  438.         if(state == 'f')
  439.           status[i] = 1;
  440.         if(state == 'r')
  441.           status[i] = 2;
  442.         if(state == 'n')
  443.           status[i] = 3;
  444.         if(status[i] == 4)
  445.         {
  446.           errorinfile(data_name, "Flag not one of FRDN", 1+2*i);
  447.           exit(0);
  448.         }
  449.     
  450.         /* Get final position */
  451.         if(end < 0 || end > 26)
  452.         {
  453.           errorinfile(data_name, "Illegal finishing position", 1+2*i);
  454.           exit(0);
  455.         }
  456.         position[i] = end;
  457.     
  458.         /* Warmup positions */
  459.         if(warm < 0 || warm > 26)
  460.         {
  461.           errorinfile(data_name, "Illegal warmup position", 1+2*i);
  462.           exit(0);
  463.         }
  464.         warmup[i] = warm;
  465.         if(warm != 0 && status[i] != 0)
  466.           totwarm+=1;
  467.       
  468.         if(nom != '-' && nom != '+')
  469.         {
  470.           errorinfile(data_name, "Nominated flag not - or +", 1+2*i);
  471.           exit(0);
  472.         }
  473.       
  474.         if(nom != '-')
  475.           if(nom == '+' && nominated == 0)
  476.             nominated = grid[i];
  477.           else
  478.           {
  479.             errorinfile(data_name, "Too many nominated drivers", 1+2*i);
  480.             exit(0);
  481.           }
  482.         
  483.         readline(store, inlen, data_file);
  484.       
  485.       }
  486.       while(!feof(data_file));
  487.   
  488.       ndrivers = i;
  489.       fclose(data_file);
  490.     }
  491.   
  492.     /* Check for correct number of warmup positions */
  493.     if(totwarm < 6)
  494.     {
  495.       errorinfile(data_name, "Not enough warmup positions", 0);
  496.       exit(0);
  497.     }
  498.  
  499.     /* clear scores */
  500.     for(i = 0; i < DRIVERS; i+=1)
  501.     {
  502.       topsixpts[i] = 0;
  503.       awardpts[i] = 0;
  504.       incgridpts[i] = 0;
  505.       warmuppts[i] = 0;
  506.       retirepts[i] = 0;
  507.       noqualpts[i] = 0;
  508.       racesc[i] = 0;
  509.     }
  510.   
  511.     /* points for finishing in top six */
  512.     k = 1;
  513.     pos = 1;
  514.     point = 1;
  515.     do
  516.     {
  517.       while(position[k] != point)
  518.         k = k + 1;
  519.       if(status[k] == 1)
  520.       {
  521.         topsixpts[grid[k]] = 10 + points[pos];
  522.         pos+=1;
  523.       }
  524.       point+=1;
  525.       k = 1;
  526.     }
  527.     while(pos < 7 && point <= ndrivers);
  528.  
  529.     /* points for increasing on grid position */
  530.     for(i = 1; i < ndrivers+1; i+=1) /* look through grid... */
  531.       if(status[i] == 1)             /* ...for drivers who finish... */
  532.         if(position[i] < i)          /* ...and beat their grid position */
  533.           incgridpts[grid[i]] = (i-position[i]);
  534.  
  535.     /* adjust grid position increase due to disqualifications */
  536.     for(i = 1; i < ndrivers+1; i+=1)    /* check all drivers for... */
  537.       if(status[i] == 0)                /* ...disqualification and... */
  538.         for(j = 1; j <ndrivers+1; j+=1) /* ...adjust the scores of... */
  539.           if(position[j] > position[i] && status[j] == 1 && position[j] <= j)
  540.             incgridpts[grid[j]]+=1;     /* ...those who finish after them */
  541.   
  542.     /* nominated driver points */
  543.     awardpts[nominated] = 5;
  544.  
  545.     /* points for first 6 places in Sunday warmup */
  546.     pos = 1;
  547.     point = 1;
  548.     i = 1;
  549.     do
  550.     {
  551.       while(warmup[i] != point) 
  552.         i+=1; /* if a driver came at position 'point' in the warmup */
  553.       if(status[i] == 1 || status[i] == 2) /* and was not disqualified */
  554.       {
  555.         warmuppts[grid[i]] = 7 - pos;
  556.         pos+=1;
  557.       }
  558.       point+=1;
  559.       i = 1;
  560.     }
  561.     while(pos < 7 && point <= ndrivers);
  562.  
  563.     /* find position of last driver (usually 26) */
  564.     last = 0;
  565.     for(i = 1; i < ndrivers+1; i+=1)
  566.       if(status[i] == 2)       /* if a driver retired */
  567.         if(position[i] > last) /* and has the highest position number */
  568.           last = position[i];  /* then he was in last place */
  569.   
  570.     /* points for first 5 to retire */
  571.     for(i = 1; i < ndrivers+1; i+=1)
  572.     {
  573.       if(status[i] == 2)
  574.       {
  575.         if(position[i] == last)
  576.           retirepts[grid[i]] = 5;
  577.         if(position[i] == last-1)
  578.           retirepts[grid[i]] = 4;
  579.         if(position[i] == last-2)
  580.           retirepts[grid[i]] = 3;
  581.         if(position[i] == last-3)
  582.           retirepts[grid[i]] = 2;
  583.         if(position[i] == last-4)
  584.           retirepts[grid[i]] = 1;
  585.       }
  586.     }
  587.  
  588.     /* points for non-qualification */
  589.     for(i = 1; i < ndrivers+1; i+=1)
  590.       if(status[i] == 3)
  591.         noqualpts[grid[i]] = 5;
  592.       
  593.     /* move non-qualifiers to end of results */
  594.     pos = last+1;
  595.     for(i = 1; i < ndrivers+1; i+=1)
  596.     {
  597.       if(status[i] == 3)
  598.       {
  599.         position[i] = pos;
  600.         pos+=1;
  601.       }
  602.     }
  603.   
  604.     /* reset chassis/engine flags/scores */
  605.     for(i = 0; i < ENGINES; i+=1)
  606.     {
  607.       eflags[i] = 0;  
  608.       engscr[i] = 0;
  609.     }
  610.  
  611.     for(i = 0; i < CHASSIS; i+=1)
  612.     {
  613.       cflags[i] = 0;
  614.       carscr[i] = 0;  
  615.     }
  616.   
  617.     for(i = 0; i < DRIVERS; i+=1)
  618.     {
  619.       carpts[i] = 0;
  620.       engpts[i] = 0;
  621.     }
  622.   
  623.     pos = 1;
  624.     point = 1;
  625.     i = 1;
  626.     do
  627.     {
  628.       while(position[i] != point)
  629.         i+=1; /* find driver who finished in position 'point' */
  630.       if(cflags[drcar[i]] == 0 && status[i] == 1)
  631.       {
  632.         /* if car hasn't already scored and driver did finish award points */
  633.      /* carpts[i] = points[pos]*2; */          /* Version 1 rules */
  634.         carpts[i] = points[pos]+10;            /* Version 2 rules */
  635.      /* carscr[drcar[i]] += points[pos]*2; */  /* Version 1 rules */
  636.         carscr[drcar[i]] += points[pos]+10;    /* Version 2 rules */
  637.         cflags[drcar[i]] = 1;
  638.         pos+=1;
  639.       }
  640.     point+=1;
  641.       i = 1;
  642.     }
  643.     while( pos < 7 && point <= ndrivers);
  644.  
  645.     pos = 1;
  646.     point = 1;
  647.     i = 1;
  648.     do
  649.     {
  650.       while(position[i] != point)
  651.         i+=1;
  652.       if(eflags[dreng[i]] == 0 && status[i] == 1)
  653.       {
  654.      /* engpts[i] = points[pos]*2; */          /* Version 1 rules */
  655.         engpts[i] = points[pos]+10;            /* Version 2 rules */
  656.      /* engscr[dreng[i]] += points[pos]*2; */  /* Version 1 rules */
  657.         engscr[dreng[i]] += points[pos]+10;    /* Version 2 rules */
  658.         eflags[dreng[i]] = 1;
  659.         pos+=1;
  660.       }
  661.       point+=1;
  662.       i = 1;
  663.     }
  664.     while( pos < 7 && point <= ndrivers);
  665.  
  666.     /* reset chassis flags */
  667.     for(i = 0; i < CHASSIS; i+=1)
  668.     {  
  669.       cflags[i] = 0;
  670.     }
  671.  
  672.     pos = last;
  673.     point = last;
  674.     i = 1;
  675.     do
  676.     {
  677.       while(position[i] != point)
  678.         i+=1;
  679.       if(cflags[drcar[i]] == 0 && status[i] == 2)
  680.       {
  681.         carpts[i] = -(pos-(last-5));
  682.         carscr[drcar[i]] -= (pos-(last-5));
  683.         cflags[drcar[i]] = 1;
  684.         pos-=1;
  685.       }
  686.       point-=1;
  687.       i = 1;
  688.     }
  689.     while(pos > last-5 && point > 0);
  690.  
  691.     scores_file = fopen(scores_name, "r");
  692.     if(scores_file == 0)
  693.     {
  694.       /* If the scores file doesn't exists, create it */
  695.       scores_file = fopen(scores_name, "w");
  696.       exists_flag = 0;
  697.     } 
  698.     else 
  699.     {
  700.       /* if it does exists, open a temporary file to move the scores to */
  701.       fclose(scores_file);
  702.       scores_file = fopen(temp_name, "w");
  703.       exists_flag = 1;
  704.       old_scores = fopen(scores_name, "r");
  705.     
  706.       /* get the original scores and move them to the temp file until we
  707.          find a race with the same name or the end of the file */
  708.       readline(racecheck, inlen, old_scores);
  709.     
  710.       while(strcasencmp(racecheck, racename, 6) && !feof(old_scores))
  711.       {
  712.         readline(store, inlen, old_scores);
  713.         racedrivers = atoi(store);
  714.         fprintf(scores_file, "%s\n", racecheck);
  715.         fprintf(scores_file, "%d\n", racedrivers);
  716.         for(i = 1; i <= racedrivers; i+=1)
  717.         {
  718.           fgets(store, inlen, old_scores);
  719.           fputs(store, scores_file);
  720.         }
  721.         readline(racecheck, inlen, old_scores);
  722.       }
  723.     
  724.       if(!feof(old_scores))
  725.       {
  726.         /* If we haven't reached the end-of-file compare the old results
  727.            with the new results */
  728.         readline(store, inlen, old_scores);
  729.         racedrivers = atoi(store);
  730.         for(i = 1; i <= racedrivers; i+=1)
  731.         {
  732.           fscanf(old_scores, "%d %d %d", &old_grid, &old_car, &old_eng);
  733.           readline(store, inlen, old_scores);
  734.           if(old_car != whichcar[old_grid])
  735.           {
  736.             /* a driver had a different car before - send a non-fatal
  737.                message explaining this */
  738.             printf("** %s's car has changed from %s to %s.\n", 
  739.               namea[old_grid], chassisa[old_car], 
  740.               chassisa[whichcar[old_grid]]);
  741.           }
  742.           if(old_eng != whicheng[old_grid])
  743.           {
  744.             printf("** %s's engine has changed from %s to %s.\n", 
  745.               namea[old_grid], enginesa[old_eng], 
  746.               enginesa[whicheng[old_grid]]);
  747.           }
  748.         }
  749.       }
  750.     }
  751.   
  752.     /* put the new scores in the correct file */
  753.     fprintf(scores_file, "%s\n", racename);
  754.     fprintf(scores_file, "%d\n", ndrivers);
  755.     for(i = 1; i <= ndrivers; i+=1)
  756.     {
  757.       fprintf(scores_file, "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
  758.         grid[i], drcar[i], dreng[i], position[i], status[i], 
  759.         topsixpts[grid[i]], awardpts[grid[i]], incgridpts[grid[i]],
  760.         warmuppts[grid[i]], retirepts[grid[i]], noqualpts[grid[i]],
  761.         carpts[i], carscr[drcar[i]], engpts[i], engscr[dreng[i]]);
  762.     }
  763.  
  764.     if(exists_flag == 1)
  765.     {
  766.       readline(racecheck, inlen, old_scores);
  767.       while(!feof(old_scores))
  768.       {
  769.         fprintf(scores_file, "%s\n", racecheck);
  770.         readline(store, inlen, old_scores);
  771.         racedrivers = atoi(store);
  772.         fprintf(scores_file, "%d\n", racedrivers);
  773.         for(i = 1; i <= racedrivers; i+=1)
  774.         {
  775.           fgets(store, inlen, old_scores);
  776.           fputs(store, scores_file);
  777.         }
  778.         readline(racecheck, inlen, old_scores);
  779.       }
  780.       fclose(old_scores);
  781.       if(unlink(scores_name) == -1)
  782.       {
  783.         fputs("Unable to delete \"scores.fgp2\".\n", stderr);
  784.         exit(0);
  785.       }
  786.     }
  787.   
  788.     fclose(scores_file);
  789.   
  790.     if(exists_flag == 1)
  791.       if(rename(temp_name, scores_name) == -1)
  792.       {
  793.         fputs("Unable to rename \"_tempscores\".\n", stderr);
  794.         exit(0);
  795.       }
  796.   }
  797.     
  798.   /* Open teams.fgp2 */
  799.   teams_file = fopen(teams_name, "r");
  800.  
  801.   /* Get the number of teams competing */
  802.   readline(store, inlen, teams_file);
  803.   nteams = atoi(store);
  804.  
  805.   for(i = 1; i < nteams + 1; i+=1)
  806.   {
  807.     readline(store, inlen, teams_file); 
  808.     strcpy(owner[i], store);
  809.     readline(store, inlen, teams_file);
  810.     strcpy(teamname[i], store);
  811.     readline(store, inlen, teams_file);
  812.     driver1[i] = driver2no(store);
  813.     readline(store, inlen, teams_file);
  814.     driver2[i] = driver2no(store);
  815.     readline(store, inlen, teams_file);
  816.     driver3[i] = driver2no(store);
  817.     readline(store, inlen, teams_file);
  818.     chassis[i] = chassis2no(store);
  819.     readline(store, inlen, teams_file);
  820.     engine[i] = engine2no(store);
  821.   }
  822.  
  823.   fclose(teams_file);
  824.  
  825.   if(normal != 255)
  826.   {
  827.     j = 0;
  828.     if(normal == 0)
  829.       normal = 255;
  830.     
  831.     scores_file = fopen(scores_name, "r");
  832.     if(scores_file == 0)
  833.     {
  834.       disperr(scores_name);
  835.       exit(0);
  836.     }
  837.     
  838.     for(i = 0; i <= MAXTEAM; i+=1)
  839.     {
  840.       scores[i] = 0;
  841.     }
  842.  
  843.     readline(store, inlen, scores_file);
  844.     while(j != normal && !feof(scores_file))
  845.     {
  846.       /* Clear arrays */
  847.       for(i = 1; i < DRIVERS; i+=1)
  848.       {
  849.         grid[i] = 0;
  850.         position[i] = 0;
  851.         status[i] = 0;
  852.         drcar[i] = 0;
  853.         dreng[i] = 0;
  854.         warmup[i] = 0;
  855.         topsixpts[i] = 0;
  856.         awardpts[i] = 0;
  857.         incgridpts[i] = 0;
  858.         warmuppts[i] = 0;
  859.         retirepts[i] = 0;
  860.         noqualpts[i] = 0;
  861.         carpts[i] = 0;
  862.         engpts[i] = 0;
  863.       }
  864.       
  865.       for(i = 0; i <= CHASSIS; i+=1)
  866.       {
  867.         carscr[i] = 0;
  868.       }
  869.       
  870.       for(i = 0; i <= ENGINES; i+=1)
  871.       {
  872.         engscr[i] = 0;
  873.       }
  874.       
  875.       strcpy(racename, store);
  876.       readline(store, inlen, scores_file);
  877.       ndrivers = atoi(store);
  878.       
  879.       for(i = 1; i <= ndrivers; i+=1)
  880.       {
  881.         fscanf(scores_file, "%d %d %d", &grid[i], &drcar[i], &dreng[i]);
  882.         fscanf(scores_file, " %d %d %d %d %d %d %d %d %d %d %d %d",
  883.           &position[i], &status[i], &topsixpts[grid[i]], 
  884.           &awardpts[grid[i]], &incgridpts[grid[i]], &warmuppts[grid[i]], 
  885.           &retirepts[grid[i]], &noqualpts[grid[i]], &carpts[i], 
  886.           &carscr[drcar[i]], &engpts[i], &engscr[dreng[i]]);
  887.         readline(store, inlen, scores_file);
  888.       }
  889.  
  890.       /* total up scores */
  891.       for(k = 0; k < DRIVERS; k+=1)
  892.         racesc[k]=topsixpts[k]+awardpts[k]+incgridpts[k]+warmuppts[k]-
  893.           retirepts[k]-noqualpts[k];
  894.  
  895.       /* Calculate teams' scores */
  896.       for(k = 1; k <= nteams; k+=1)
  897.       {
  898.         thisrce[k]=(racesc[driver1[k]]+racesc[driver2[k]]+
  899.           racesc[driver3[k]]+carscr[chassis[k]]+engscr[engine[k]]);
  900.         scores[k]+=thisrce[k];
  901.       }
  902.       
  903.       j+=1;
  904.       readline(store, inlen, scores_file);
  905.     }
  906.     
  907.     fclose(scores_file);
  908.  
  909.     printf("%s\n", verst);
  910.     capitalise(racename);
  911.     printf("%s Starting Grid\n\n", racename);
  912.     for(i = 1; i < ndrivers+1; i+=1) 
  913.     {
  914.       if(status[i] != 3)
  915.       {
  916.         sprintf(fullname, "%s %s", fnama[grid[i]], namea[grid[i]]);
  917.         printf("%2d : %-30s", i, fullname);
  918.         printf(" %s-", chassisa[drcar[i]]);
  919.         printf("%s\n", enginesa[dreng[i]]);
  920.       } 
  921.     }
  922.     printf("\n%s Results\n\n", racename);
  923.     printf("    NAME      POINTS BREAKDOWN       CHASSIS    POINTS ENGINE");
  924.     printf("        POINTS\n");
  925.     printf("                      Top Six\n");
  926.     printf("                      |  Award                Driver's          ");
  927.     printf("   Driver's\n");
  928.     printf("                      |  | Grid Increase       Chassis          ");
  929.     printf("     Engine\n");
  930.     printf("                      |  | |  Warm-up               |           ");
  931.     printf("         |\n");
  932.     printf("                      |  | |  | Retirement          |           ");
  933.     printf("         |\n");
  934.     printf("                      |  | |  | | Non-qualification |           ");
  935.     printf("         |\n");
  936.     printf("                      |  | |  | | |                 |           ");
  937.     printf("         |\n");
  938.  
  939.     pos = 1;
  940.     j = 1;
  941.     do
  942.     {
  943.       while(position[j] != pos)
  944.         j+=1;
  945.       if(status[j] == 0)
  946.         strcpy(code, "DIS");
  947.       if(status[j] == 1)
  948.         sprintf(code, "%-3d", pos);
  949.       if(status[j] == 2)
  950.         strcpy(code, "rtd");
  951.       if(status[j] == 3)
  952.         strcpy(code, "DNQ");
  953.          
  954.       printf("%s %-13s %2d (%02d+%1d+%02d+%1d-%1d-%1d) ", code, namea[grid[j]],
  955.         racesc[grid[j]], topsixpts[grid[j]], awardpts[grid[j]], 
  956.         incgridpts[grid[j]], warmuppts[grid[j]], retirepts[grid[j]],
  957.         noqualpts[grid[j]]);
  958.       printf("%-9s %2d [%2d]", 
  959.         chassisa[drcar[j]], carscr[drcar[j]], carpts[j]);
  960.       printf(" %-12s %2d [%2d]\n", 
  961.         enginesa[dreng[j]], engscr[dreng[j]], engpts[j]);
  962.       pos+=1;
  963.       j = 1;
  964.     }
  965.     while(pos < ndrivers+1); 
  966.  
  967.     for(i = 1; i < MAXTEAM; i+=1)
  968.       sflags[i] = 0;
  969.  
  970.     printf("\n%s Team Scores\n\n", racename);
  971.  
  972.     for(i = 1; i < nteams+1; i+=1)
  973.     {
  974.       highest = 0;
  975.       highscore = -500;
  976.       for(j = 1; j < nteams+1; j+=1)
  977.         if(sflags[j] == 0)
  978.           if(scores[j] > highscore)
  979.           {
  980.             highscore = scores[j];
  981.             highest = j;
  982.           }
  983.       sflags[highest] = 1;
  984.       /* getteam(highest); */
  985.       printf("Team Owner   : %s\nTeam Name    : %s\n", owner[highest],
  986.         teamname[highest]);
  987.       printf("Series Score : %3d\n", scores[highest]); 
  988.       printf("Race Score   : %3d\n", thisrce[highest]);
  989.       printf("Race Details : Driver one    - %-13s = %2d\n", namea[driver1[highest]], 
  990.         racesc[driver1[highest]]);
  991.       printf("               Driver two    - %-13s = %2d\n", namea[driver2[highest]], 
  992.         racesc[driver2[highest]]);
  993.       printf("               Test Driver   - %-13s = %2d\n", namea[driver3[highest]], 
  994.         racesc[driver3[highest]]);
  995.       printf("               Car's Chassis - %-13s = %2d\n", 
  996.         chassisa[chassis[highest]], carscr[chassis[highest]]);
  997.       printf("               Car's Engine  - %-13s = %2d\n\n", 
  998.         enginesa[engine[highest]], engscr[engine[highest]]);
  999.     }
  1000.   }
  1001.  
  1002.   if(teams != 255)
  1003.   {
  1004.     j = 0;
  1005.     if(teams == 0)
  1006.       teams = 255;
  1007.     
  1008.     scores_file = fopen(scores_name, "r");
  1009.     if(scores_file == 0)
  1010.     {
  1011.       disperr(scores_name);
  1012.       exit(0);
  1013.     }
  1014.     
  1015.     for(i = 0; i <= MAXTEAM; i+=1)
  1016.     {
  1017.       scores[i] = 0;
  1018.     }
  1019.  
  1020.     readline(store, inlen, scores_file);
  1021.     while(j != teams && !feof(scores_file))
  1022.     {
  1023.       /* Clear arrays */
  1024.       for(i = 1; i < DRIVERS; i+=1)
  1025.       {
  1026.         grid[i] = 0;
  1027.         position[i] = 0;
  1028.         status[i] = 0;
  1029.         drcar[i] = 0;
  1030.         dreng[i] = 0;
  1031.         warmup[i] = 0;
  1032.         topsixpts[i] = 0;
  1033.         awardpts[i] = 0;
  1034.         incgridpts[i] = 0;
  1035.         warmuppts[i] = 0;
  1036.         retirepts[i] = 0;
  1037.         noqualpts[i] = 0;
  1038.         carpts[i] = 0;
  1039.         engpts[i] = 0;
  1040.         racesc[i] = 0;
  1041.       }
  1042.       
  1043.       for(i = 0; i <= CHASSIS; i+=1)
  1044.       {
  1045.         carscr[i] = 0;
  1046.       }
  1047.       
  1048.       for(i = 0; i <= ENGINES; i+=1)
  1049.       {
  1050.         engscr[i] = 0;
  1051.       }
  1052.       
  1053.       capitalise(store);
  1054.       strcpy(racenames[j], store);
  1055.       readline(store, inlen, scores_file);
  1056.       ndrivers = atoi(store);
  1057.       
  1058.       for(i = 1; i <= ndrivers; i+=1)
  1059.       {
  1060.         fscanf(scores_file, "%d %d %d", &grid[i], &drcar[i], &dreng[i]);
  1061.         fscanf(scores_file, " %d %d %d %d %d %d %d %d %d %d %d %d",
  1062.           &position[i], &status[i], &topsixpts[grid[i]], 
  1063.           &awardpts[grid[i]], &incgridpts[grid[i]], &warmuppts[grid[i]], 
  1064.           &retirepts[grid[i]], &noqualpts[grid[i]], &carpts[i], 
  1065.           &carscr[drcar[i]], &engpts[i], &engscr[dreng[i]]);
  1066.         readline(store, inlen, scores_file);
  1067.       }
  1068.  
  1069.       /* total up scores */
  1070.       for(k = 0; k < DRIVERS; k+=1)
  1071.         racesc[k]=topsixpts[k]+awardpts[k]+incgridpts[k]+warmuppts[k]-
  1072.           retirepts[k]-noqualpts[k];
  1073.  
  1074.       /* Calculate teams' scores */
  1075.       for(k = 1; k <= nteams; k+=1)
  1076.       {
  1077.         teamscores[k][j]=(racesc[driver1[k]]+racesc[driver2[k]]+
  1078.           racesc[driver3[k]]+carscr[chassis[k]]+engscr[engine[k]]);
  1079.         scores[k]+=teamscores[k][j];
  1080.       }
  1081.       
  1082.       j+=1;
  1083.       readline(store, inlen, scores_file);
  1084.     }
  1085.     nraces = j-1;
  1086.     
  1087.     fclose(scores_file);
  1088.  
  1089.     printf("%s\n", verst);
  1090.     printf("Team scores table\n");
  1091.     
  1092.     for(i = 0; i <= nraces; i+=1)
  1093.     {
  1094.       printf("                        ");
  1095.       if(i > 0)
  1096.         for(j = 0; j < i; j+=1)
  1097.           printf(" |  ");
  1098.       printf("%s\n", racenames[i]);
  1099.     }
  1100.     
  1101.     printf("+----------------------+");
  1102.     for(i = 0; i <= nraces; i+=1)
  1103.       printf("---+");
  1104.     printf("-----+\n");
  1105.     
  1106.     for(i = 1; i <= nteams; i+=1)
  1107.     {
  1108.       printf("| %-20s |", teamname[i]);
  1109.       for(j = 0; j <= nraces; j+=1)
  1110.         printf("%3d|", teamscores[i][j]);
  1111.       printf("%5d|\n", scores[i]);
  1112.     
  1113.       printf("+----------------------+");
  1114.       for(k = 0; k <= nraces; k+=1)
  1115.         printf("---+");
  1116.       printf("-----+\n");
  1117.     }
  1118.     printf("\n");
  1119.   }
  1120.  
  1121.   if(drive != 255)
  1122.   {
  1123.     j = 0;
  1124.     if(drive == 0)
  1125.       drive = 255;
  1126.     
  1127.     scores_file = fopen(scores_name, "r");
  1128.     if(scores_file == 0)
  1129.     {
  1130.       disperr(scores_name);
  1131.       exit(0);
  1132.     }
  1133.  
  1134.     for(i = 0; i <= MAXTEAM; i+=1)
  1135.     {
  1136.       scores[i] = 0;
  1137.     }
  1138.     
  1139.     readline(store, inlen, scores_file);
  1140.     while(j != drive && !feof(scores_file))
  1141.     {
  1142.       /* Clear arrays */
  1143.       for(i = 1; i < DRIVERS; i+=1)
  1144.       {
  1145.         grid[i] = 0;
  1146.         position[i] = 0;
  1147.         status[i] = 0;
  1148.         drcar[i] = 0;
  1149.         dreng[i] = 0;
  1150.         warmup[i] = 0;
  1151.         topsixpts[i] = 0;
  1152.         awardpts[i] = 0;
  1153.         incgridpts[i] = 0;
  1154.         warmuppts[i] = 0;
  1155.         retirepts[i] = 0;
  1156.         noqualpts[i] = 0;
  1157.         carpts[i] = 0;
  1158.         engpts[i] = 0;
  1159.       }
  1160.       
  1161.       for(i = 0; i <= CHASSIS; i+=1)
  1162.       {
  1163.         carscr[i] = 0;
  1164.       }
  1165.       
  1166.       for(i = 0; i <= ENGINES; i+=1)
  1167.       {
  1168.         engscr[i] = 0;
  1169.       }
  1170.       
  1171.       capitalise(store);
  1172.       strcpy(racenames[j], store);
  1173.       readline(store, inlen, scores_file);
  1174.       ndrivers = atoi(store);
  1175.       
  1176.       for(i = 1; i <= ndrivers; i+=1)
  1177.       {
  1178.         fscanf(scores_file, "%d %d %d", &grid[i], &drcar[i], &dreng[i]);
  1179.         fscanf(scores_file, " %d %d %d %d %d %d %d %d %d %d %d %d",
  1180.           &position[i], &status[i], &topsixpts[grid[i]], 
  1181.           &awardpts[grid[i]], &incgridpts[grid[i]], &warmuppts[grid[i]], 
  1182.           &retirepts[grid[i]], &noqualpts[grid[i]], &carpts[i], 
  1183.           &carscr[drcar[i]], &engpts[i], &engscr[dreng[i]]);
  1184.         readline(store, inlen, scores_file);
  1185.       }
  1186.  
  1187.       /* total up scores */
  1188.       for(k = 0; k < DRIVERS; k+=1)
  1189.       {
  1190.         driverscore[k][j]=topsixpts[k]+awardpts[k]+incgridpts[k]
  1191.           +warmuppts[k]-retirepts[k]-noqualpts[k];
  1192.         drivertotal[k]+=driverscore[k][j];
  1193.       }
  1194.       
  1195.       j+=1;
  1196.       readline(store, inlen, scores_file);
  1197.     }
  1198.     nraces = j-1;
  1199.     
  1200.     fclose(scores_file);
  1201.  
  1202.     printf("%s\n", verst);
  1203.     printf("Driver scores table\n");
  1204.      
  1205.     for(i = 0; i <= nraces; i+=1)
  1206.     {
  1207.       printf("                        ");
  1208.       if(i > 0)
  1209.         for(j = 0; j < i; j+=1)
  1210.           printf(" |  ");
  1211.       printf("%s\n", racenames[i]);
  1212.     }
  1213.     
  1214.     printf("+----------------------+");
  1215.     for(i = 0; i <= nraces; i+=1)
  1216.       printf("---+");
  1217.     printf("-----+\n");
  1218.     
  1219.     for(i = 1; i <= maxdrivers; i+=1)
  1220.     {
  1221.       printf("| %-20s |", namea[i]);
  1222.       for(j = 0; j <= nraces; j+=1)
  1223.         printf("%3d|", driverscore[i][j]);
  1224.       printf("%5d|\n", drivertotal[i]);
  1225.     
  1226.       printf("+----------------------+");
  1227.       for(k = 0; k <= nraces; k+=1)
  1228.         printf("---+");
  1229.       printf("-----+\n");
  1230.     }
  1231.     printf("\n");
  1232.   }
  1233.   
  1234.   exit(0);
  1235. }
  1236.  
  1237. void disperr(char *file_name)
  1238. {
  1239.   fputs(verst, stderr);
  1240.   fputs("Unable to open \"", stderr);
  1241.   fputs(file_name, stderr);
  1242.   fputs("\".\n", stderr);
  1243.   return;
  1244. }
  1245.  
  1246. void errorinfile(char *file_name, char *error, int line)
  1247. {
  1248.   fputs(verst, stderr);
  1249.   fputs("Error in file \"", stderr);
  1250.   fputs(file_name, stderr);
  1251.   fputs("\" on line ", stderr);
  1252.   fprintf(stderr, "%d", line);
  1253.   fputs(". ", stderr);
  1254.   fputs(error, stderr);
  1255.   fputs(".\n", stderr);
  1256. }  
  1257.  
  1258. int driver2no(char *driname)
  1259. {
  1260.   int  n;
  1261.  
  1262.   n = 1; 
  1263.   while((strcasencmp(namea[n], driname, 6)) && n <= maxdrivers)
  1264.     n+=1;
  1265.     
  1266.   if(n > maxdrivers)
  1267.     n = 0;
  1268.   
  1269.   return(n);
  1270. }
  1271.  
  1272. int chassis2no(char *chaname)
  1273. {
  1274.   int n;
  1275.   
  1276.   n = 1;
  1277.   while((strcasencmp(chassisa[n], chaname, 6)) && n <= maxchassis)
  1278.     n+=1;
  1279.   
  1280.   if(n > maxchassis)
  1281.     n=0;
  1282.     
  1283.   return(n);
  1284. }
  1285.   
  1286. int engine2no(char *engname)
  1287. {
  1288.   int n;
  1289.   
  1290.   n = 1;
  1291.   while((strcasencmp(enginesa[n], engname, 6)) && n <= maxengines)
  1292.     n+=1;
  1293.   
  1294.   if(n > maxengines)
  1295.     n=0;
  1296.     
  1297.   return(n);
  1298. }
  1299.   
  1300. int strcasencmp(char *stringa, char *stringb, int testnum)
  1301. {
  1302.   int  i;
  1303.   char string1[100], string2[100];
  1304.   
  1305.   strcpy(string1, stringa);
  1306.   strcpy(string2, stringb);
  1307.   
  1308.   for(i = 0; i <= strlen(string1); i+=1)
  1309.     string1[i] = tolower(string1[i]);
  1310.   for(i = 0; i <= strlen(string2); i+=1)
  1311.     string2[i] = tolower(string2[i]);
  1312.     
  1313.   return(strncmp(string1, string2, testnum));
  1314. }
  1315.  
  1316. void capitalise(char *word)
  1317. {
  1318.   word[0] = toupper(word[0]);
  1319.   return;
  1320. }
  1321.  
  1322. unsigned int readline(char *in_string, int chars_2_read, FILE *file_pointer)
  1323. {
  1324.   char temp[100];
  1325.   
  1326.   fgets(temp, chars_2_read, file_pointer);
  1327.   
  1328.   while(temp[0] == '#' && temp[1] == '#')
  1329.     fgets(temp, chars_2_read, file_pointer);
  1330.   
  1331.   strcpy(in_string, temp);
  1332.   in_string[strlen(in_string)-1] = 0;
  1333.   
  1334.   return(strlen(temp)-1);
  1335. }
  1336.